<?php

class DoctrineODM extends DoctrineObjectModel {
	
	public function __construct($options = array()) {
		Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses();
		$this->config = new Doctrine\ODM\MongoDB\Configuration();
		
		$this->option('setProxyDir', __DIR__ . '/Proxies')->option('setProxyNamespace', 'Proxies');
		$this->option('setHydratorDir', __DIR__ . '/Hydrators')->option('setHydratorNamespace', 'Hydrators');
		$this->option('setDocumentNamespaces', array('Documents'));

		foreach ($options as $option => $value) {
			if ($option{0} == ':') continue;
			$this->option($option, $value);
		}
		
		$driver = new Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver(empty($options[':conf']) ? array(__DIR__ . '/conf') : $options[':conf']);
		$this->option('setMetadataDriverImpl', $driver);
		
		$this->manager = Doctrine\ODM\MongoDB\DocumentManager::create(new Doctrine\MongoDB\Connection(), $this->config);
	}
	
	public function cli() {
		if (empty($this->manager)) throw new Exception('DocumentManager object is empty');
		
		$helperSet = new \Symfony\Component\Console\Helper\HelperSet();
		$helperSet->set(new Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper($this->manager), 'dm');
		
		$cli = new \Symfony\Component\Console\Application('Doctrine ODM MongoDB Command Line Interface', Doctrine\ODM\MongoDB\Version::VERSION);
		$cli->setCatchExceptions(true);
		$cli->setHelperSet($helperSet);
		$cli->addCommands(array(
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand(),
			new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand(),
		));
		$cli->run();
	}
}
